package com.zbb.jacocoserver.controller;

import com.zbb.jacocoserver.common.IdGenerator;
import com.bestpay.jacocoserver.model.*;
import com.zbb.jacocoserver.service.BuildInfoService;
import com.zbb.jacocoserver.service.JacocoService;
import com.zbb.jacocoserver.model.BuildInfo;
import com.zbb.jacocoserver.model.BuildKey;
import com.zbb.jacocoserver.model.CoverageParam;
import com.zbb.jacocoserver.model.Response;
import lombok.extern.slf4j.Slf4j;
import org.eclipse.jgit.api.errors.GitAPIException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.nio.file.Paths;


@Slf4j
@RestController
@RequestMapping("/coverage")
public class JacocoController {
    @Autowired
    private JacocoService jacocoService;
    @Autowired
    private BuildInfoService buildInfoService;


    @PostMapping("/all")
    public Response<BuildKey> executeForAllCoverage(@RequestBody CoverageParam coverageParam){
        String buildId = IdGenerator.uuid();
        log.info("executeForAllCoverage接收到的参数{}",coverageParam.toString());
        try {
            jacocoService.executeForAllCoverage(coverageParam.getGitAccount(),coverageParam.getGitPassword(),
                    coverageParam.getGitAddress(),coverageParam.getBranch(),coverageParam.getProjectAddress(),
                    coverageParam.getModules(),buildId);
        } catch (IOException | GitAPIException e) {
            e.printStackTrace();
        }
        Response<BuildKey> response = new Response<>();
        response.setMsg("请求成功");
        response.setStatus("200");
        BuildKey buildKey = new BuildKey();
        buildKey.setBuildId(buildId);
        response.setData(buildKey);
        return response;
    }

    @PostMapping("/add")
    public Response<BuildKey> executeForAddCoverage(@RequestBody CoverageParam coverageParam){
        String buildId = IdGenerator.uuid();
        log.info("executeForAddCoverage接收到的参数{}",coverageParam.toString());
        try {
            jacocoService.executeForAddCoverage(coverageParam.getGitAccount(),coverageParam.getGitPassword(),
                    coverageParam.getGitAddress(),coverageParam.getBranch(),coverageParam.getProjectAddress(),
                    coverageParam.getModules(),buildId,coverageParam.getNewCommitId(),coverageParam.getOldCommitId());
        } catch (IOException | GitAPIException e) {
            e.printStackTrace();
        }
        Response<BuildKey> response = new Response<>();
        response.setMsg("请求成功");
        response.setStatus("200");
        BuildKey buildKey = new BuildKey();
        buildKey.setBuildId(buildId);
        response.setData(buildKey);
        return response;
    }

    @GetMapping("/download")
    public String downloadFile(HttpServletRequest request, HttpServletResponse response,String buildId){
        String fileName = buildId+".zip";// 文件名
        if (fileName != null) {
            //设置文件路径
            File file = Paths.get("e:","jacocoWorkSpace",buildId,fileName).toFile();
            if (file.exists()) {
                response.setContentType("application/force-download");// 设置强制下载不打开
                response.addHeader("Content-Disposition", "attachment;fileName=" + fileName);// 设置文件名
                byte[] buffer = new byte[1024];
                FileInputStream fis = null;
                BufferedInputStream bis = null;
                try {
                    fis = new FileInputStream(file);
                    bis = new BufferedInputStream(fis);
                    OutputStream os = response.getOutputStream();
                    int i = bis.read(buffer);
                    while (i != -1) {
                        os.write(buffer, 0, i);
                        i = bis.read(buffer);
                    }
                    return "下载成功";
                } catch (Exception e) {
                    e.printStackTrace();
                } finally {
                    if (bis != null) {
                        try {
                            bis.close();
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                    if (fis != null) {
                        try {
                            fis.close();
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                }
            }
        }
        return "下载失败";
    }

    @GetMapping("/getBuildInfo")
    public Response<BuildInfo> getBuildInfo(String buildId){
        BuildInfo buildInfo = buildInfoService.selectByBuildId(buildId);
        Response<BuildInfo> response = new Response<>();
        response.setStatus("200");
        response.setMsg("请求成功");
        response.setData(buildInfo);
        return response;
    }
}
